﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using com.lover.astd.common.config;
using com.lover.astd.common.model;
using com.lover.astd.common;

namespace com.lover.astd.game.ui.server.impl.equipment
{
    public class QhServer : LogicServer
    {
        /// <summary>
        /// 强化辅助
        /// </summary>
        public QhServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_Qh; ServerReadableName = ConfigStrings.SR_Qh; }
        public override void renderSettings()
        {
            refreshData();
            MainForm frm = _mainForm;

            Dictionary<string, string> conf = getConfig(ServerName);
            frm.chk_equip_qh_enable.Checked = conf.ContainsKey(ConfigStrings.enabled) && conf[ConfigStrings.enabled].ToLower().Equals("true");
            if (conf.ContainsKey(ConfigStrings.qh_ids)) renderDataGridToSelected<Equipment>(frm.dg_equipQh, conf[ConfigStrings.qh_ids]);
            frm.chk_equip_qh_use_gold.Checked = conf.ContainsKey(ConfigStrings.use_gold) && conf[ConfigStrings.use_gold].ToLower().Equals("true");
            if (conf.ContainsKey(ConfigStrings.use_gold_limit) && conf[ConfigStrings.use_gold_limit] != "")
            {
                int gold_limit = 0;
                Int32.TryParse(conf[ConfigStrings.use_gold_limit], out gold_limit);
                frm.num_equip_qh_use_gold_limit.Value = gold_limit;
            }
            refreshData();
        }
        private void refreshData()
        {
            if (_mainForm.dg_equipQh.DataSource == null)
            {
                BindingSource src = new BindingSource();
                src.DataSource = getUser()._qhEquips;
                _mainForm.dg_equipQh.AutoGenerateColumns = false;
                _mainForm.dg_equipQh.DataSource = src;
            }
            _mainForm.dg_equipQh.Refresh();
        }
        public override void saveSettings()
        {
            MainForm frm = _mainForm;
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_equip_qh_enable.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.qh_ids, getDataGridSelectedIds<Equipment>(frm.dg_equipQh));
            conf.setConfig(ServerName, ConfigStrings.use_gold, frm.chk_equip_qh_use_gold.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.use_gold_limit, frm.num_equip_qh_use_gold_limit.Value.ToString());
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, "true");
            conf.setConfig(ServerName, ConfigStrings.qh_ids, "");
            conf.setConfig(ServerName, ConfigStrings.use_gold, "true");
            conf.setConfig(ServerName, ConfigStrings.use_gold_limit, "1000000");
            renderSettings();
        }
        private List<int> getEquipIdsToQianghua()
        {
            Dictionary<string, string> configs = getConfig("equip_qh");
            string confstr = "";
            if (configs.ContainsKey(ConfigStrings.qh_ids)) confstr = configs[ConfigStrings.qh_ids];
            return generateIds(confstr);
        }
    }
}
